﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BackyardBunnies
{
    class UserControlledSprite : Sprite    //Derive from base class Sprite
    {

        // Constructor #1
        public UserControlledSprite(Texture2D textureImage, Vector2 position,
            Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
            Vector2 speed)
            : base(textureImage, position, frameSize, collisionOffset, currentFrame,
            currentFrame, speed, 0)
        {
        }

        // Constructor #2
        public UserControlledSprite(Texture2D textureImage, Vector2 position,
            Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
            Vector2 speed, int millisecondsPerFrame)
            : base(textureImage, position, frameSize, collisionOffset, currentFrame,
            currentFrame, speed, millisecondsPerFrame, 0)
        {
        }

        //Enables keyboard, left thumbstick and DPad
        public override Vector2 direction
        {
            get
            {
                Vector2 inputDirection = Vector2.Zero;

                //Keyboard input
                if ((Keyboard.GetState().IsKeyDown(Keys.A)) || (Keyboard.GetState().IsKeyDown(Keys.Left)))
                    inputDirection.X -= 1;
                if ((Keyboard.GetState().IsKeyDown(Keys.D)) || (Keyboard.GetState().IsKeyDown(Keys.Right)))
                    inputDirection.X += 1;
                if ((Keyboard.GetState().IsKeyDown(Keys.W)) || (Keyboard.GetState().IsKeyDown(Keys.Up)))
                    inputDirection.Y -= 1;
                if ((Keyboard.GetState().IsKeyDown(Keys.S)) || (Keyboard.GetState().IsKeyDown(Keys.Down)))
                    inputDirection.Y += 1;
                
                if ((Keyboard.GetState().IsKeyDown(Keys.Space)))
                {
                    return inputDirection * speed * 2;
                }

                //Gamepad: Left Thumbstick
                GamePadState gamepadState = GamePad.GetState(PlayerIndex.One);
                if (gamepadState.ThumbSticks.Left.X != 0)
                    inputDirection.X += gamepadState.ThumbSticks.Left.X;
                if (gamepadState.ThumbSticks.Left.Y != 0)
                    inputDirection.Y -= gamepadState.ThumbSticks.Left.Y;

                //DPad
                if (gamepadState.DPad.Left == ButtonState.Pressed)
                    inputDirection.X -= 1;
                if (gamepadState.DPad.Right == ButtonState.Pressed)
                    inputDirection.X += 1;
                if (gamepadState.DPad.Up == ButtonState.Pressed)
                    inputDirection.Y -= 1;
                if (gamepadState.DPad.Down == ButtonState.Pressed)
                    inputDirection.Y += 1;

                //Go Fast & Force Feedback
                if (gamepadState.Buttons.A == ButtonState.Pressed)
                {
                    GamePad.SetVibration(PlayerIndex.One, 1f, 1f);
                    return inputDirection * speed * 2;
                }

                //In-Game Secret (HYPER SPEED SHARKMAN)
                else if ((gamepadState.IsButtonDown(Buttons.LeftShoulder)) && (gamepadState.IsButtonDown(Buttons.RightShoulder)))
                {
                    GamePad.SetVibration(PlayerIndex.One, 1f, 2f);
                    return inputDirection * speed * 10;
                }
                else
                {
                    GamePad.SetVibration(PlayerIndex.One, 0f, 0f);
                    return inputDirection * speed;
                }
            }
        }

  
        public override void Update(GameTime gameTime, Rectangle clientBounds)
        {
            //Move the sprite according to the direction property
            position += direction;


            //Keeps user controlled on screen
            if (position.X < 160 - (frameSize.X / 2))
                position.X = 160 - (frameSize.X / 2);
            if (position.Y < 65)
                position.Y = 65;
            if (position.X > 713 - (frameSize.X / 2))
                position.X = 713 - (frameSize.X / 2);
            if (position.Y > 505 - (frameSize.Y / 2))
                position.Y = 505 - (frameSize.Y / 2);


            base.Update(gameTime, clientBounds);
        }
    }
}
